package com.zeny.coding.controller.admin.business;


import com.zeny.coding.constants.CourseConstants;
import com.zeny.coding.dto.ChapterDto;
import com.zeny.coding.dto.PageDto;
import com.zeny.coding.dto.Response;
import com.zeny.coding.service.ChapterService;
import com.zeny.coding.util.DtoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.bind.ValidationException;

/**
 * <h2>章节</h2>
 *
 * @Author Zeny
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/business/admin/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @PostMapping("/list/{courseId}")
    public Response list(@RequestBody PageDto<ChapterDto> pageDto, @PathVariable("courseId") String courseId) {
        pageDto.check();
        chapterService.list(pageDto, courseId);
        return new Response().success().data(pageDto).message(CourseConstants.QUERY_SUCCESS);
    }

    @PostMapping("/save")
    public Response save(@RequestBody ChapterDto chapterDto) throws ValidationException {
        chapterDto.isValidForSave();
        this.chapterService.save(chapterDto);
        return new Response().success().message(CourseConstants.SAVE_SUCCESS);
    }

    @PutMapping("/update")
    public Response update(@RequestBody ChapterDto chapterDto) throws ValidationException {
        chapterDto.isValidForUpdate();
        this.chapterService.update(chapterDto);
        return new Response().success().message(CourseConstants.UPDATE_SUCCESS);
    }

    @GetMapping("/findById/{id}")
    public Response findById(@PathVariable("id") String id) throws ValidationException {
        DtoUtil.require(id);
        ChapterDto chapterDto = this.chapterService.findById(id);
        return new Response()
                .success()
                .data(chapterDto)
                .message(CourseConstants.QUERY_SUCCESS);
    }

    @DeleteMapping("/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) throws ValidationException {
        DtoUtil.require(id);
        this.chapterService.deleteById(id);
        return new Response().success().message(CourseConstants.DELETE_SUCCESS);
    }
}
